import time
from selenium import webdriver
from selenium.webdriver.common.by import By
import os
from selenium.webdriver.support.select import Select

# 弹窗处理
class TestCase:

    def __init__(self) -> None:
        self.driver=webdriver.Chrome()
        path=os.path.dirname( os.path.abspath(__file__) )
        file_path='file:///'+path+'/17 alert.html'
        self.driver.get(file_path)
        time.sleep(3)

    def test_alert(self):
        #先触发弹窗
        self.driver.find_element(By.ID,'alert').click()
        time.sleep(2)
        #获取弹窗对象
        alert=self.driver.switch_to.alert
        #获取弹窗文本
        print(alert.text)
        #点击确定
        alert.accept()
        time.sleep(3)

    def test_confirm(self):
        self.driver.find_element(By.ID,'confirm').click()
        time.sleep(2)
        alert=self.driver.switch_to.alert
        print(alert.text)
        #点击取消
        alert.dismiss()
        #点击确定
        # alert.accept()
        time.sleep(3)

    def test_prompt(self):
        self.driver.find_element(By.ID,'prompt').click()
        time.sleep(2)
        alert=self.driver.switch_to.alert
        print(alert.text)
        #输入内容
        alert.send_keys('hello')
        time.sleep(2)
        alert.accept()
        time.sleep(3)    

if __name__ == '__main__':
    case=TestCase()        
    # case.test_alert()
    # case.test_confirm()
    case.test_prompt()